<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加角色按钮 -->
    <el-button type="primary" @click="dialogAddVisible = true"
      >添加角色</el-button
    >
    <el-table :data="RolesList" border>
      <el-table-column type="expand">
        <template slot-scope="scope">
          <div
            class="box"
            v-for="(v, i) in scope.row.children"
            :key="v.id"
            :class="{ border_bottom_none: i != scope.row.children.length - 1 }"
          >
            <div class="box_left">
              <el-tag closable type="" effect="dark">
                {{ v.authName }}
              </el-tag>
            </div>
            <div class="box_right">
              <div
                class="box_right_item"
                v-for="(vv, ii) in v.children"
                :key="vv.id"
              >
                <div
                  class="box_right_left"
                  :class="{ border_bottom_none: ii == v.children.length - 1 }"
                >
                  <el-tag closable type="success" effect="dark"
                    >{{ vv.authName }}
                  </el-tag>
                </div>
                <div
                  class="box_right_right"
                  :class="{ border_bottom_none: ii == v.children.length - 1 }"
                >
                  <el-tag
                    closable
                    type="warning"
                    effect="dark"
                    v-for="(vvv, iii) in vv.children"
                    :key="vvv.id"
                  >
                    {{ vvv.authName }}</el-tag
                  >
                </div>
              </div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="#" type="index"> </el-table-column>
      <el-table-column label="角色名称" prop="roleName"> </el-table-column>
      <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
      <el-table-column label="操作" width="300px">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="edit_role(scope.row)"
            >编辑</el-button
          >
          &nbsp;
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="Destroy(scope.row)"
            >删除</el-button
          >
          &nbsp;
          <el-button
            type="warning"
            icon="el-icon-setting"
            size="mini"
            @click="RolesGrant(scope.row)"
            >分配权限</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分配权限 ------------->
    
    <el-dialog title="分配权限" :visible.sync="dialogRolesVisible">
      <div slot="footer" class="dialog-footer">
        <el-tree
          :data="MessageRoles"
          show-checkbox
          node-key="id"
          default-expand-all
          :default-expanded-keys="[2, 3]"
          :default-checked-keys="[5]"
          :props="defaultProps"
          ref="rightTree"
        >
        </el-tree>

        <el-button @click="dialogRolesVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm_power"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 添加角色弹出框 -------------------->

    <el-dialog title="添加角色" :visible.sync="dialogAddVisible">
      <el-form :model="addform">
        <el-form-item label="角色名称">
          <el-input v-model="addform.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="addform.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAddVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRoles">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改角色弹出框 --------------->

    <el-dialog title="添加角色" :visible.sync="dialogEditVisible">
      <el-form :model="editForm">
        <el-form-item label="角色名称">
          <el-input v-model="editForm.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="editForm.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="EditRoles">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getRoles,
  Management,
  AddRoles,
  DelRoles,
  Edit_Role,
  Edit_rights,
  Role_Authorization
} from "@/http/Roles.js";
export default {
  data() {
    return {
      RolesList: [],  // 所有角色信息
      MessageRoles: [],// 权限管理数据
      dialogRolesVisible: false,//权限管理
      dialogAddVisible: false, // 添加----
      dialogEditVisible: false, // 修改---
      // 添加------------
      addform: {
        roleName: "",
        roleDesc: "",
      },
      // 修改----------
      editForm: {
        roleName: "",
        roleDesc: "",
        id: 0,
      },
     // tree树形控件 label是绑定的名称
      defaultProps: {
        children: "children",
        label: "authName",
      },
      RolesArr: [],
      roleId:0
    };
  },
  methods: {
    // 封装的所有角色信息
    async getRolesList() {
      let res = await getRoles();
      console.log(res);
      // this.tableDate = res.data;
      this.RolesList = res.data;
    },
    // 权限管理
    async RolesGrant(row) {
      let res = await Management("tree");
      this.MessageRoles = res.data;
      console.log(res);
      this.roleId=row.id
      console.log(this.roleId);
      this.recursion(row, this.RolesArr);
      this.dialogRolesVisible = true;
    },
    recursion(row, arr) {
      if (!row.children) {
        arr.push(row.id);
      } else {
        row.children.forEach((item) => this.recursion(item, arr));
      }
    },
    // 确认分配权限
    async  confirm_power(){
      console.log(this.$refs.rightTree.getCheckedKeys())
      console.log(this.$refs.rightTree.getHalfCheckedKeys())
      let rids = [...this.$refs.rightTree.getCheckedKeys(),...this.$refs.rightTree.getHalfCheckedKeys()].join(',')
      console.log(rids);
      let res = await Role_Authorization(this.roleId,{rids})
      this.dialogRolesVisible=false
      this.getRolesList();
    },
    //添加角色
    async addRoles() {
      this.dialogAddVisible = false;
      let res = await AddRoles(this.addform);
      this.getRolesList();
      console.log(res);
    },
    // 删除角色
    async Destroy(val) {
      console.log(val);
      let res = await DelRoles(val.id);
      console.log(res);
      this.getRolesList();
    },
// 通过ID查询角色 数据回填--------------------
    async edit_role(val) {
      this.dialogEditVisible = true;
      let res = await Edit_Role(val.id);
      console.log(res);
      this.editForm.id = res.data.roleId;
      this.editForm.roleName = res.data.roleName;
      this.editForm.roleDesc = res.data.roleDesc;
    },
// 修改角色--------------------
    async EditRoles() {
      console.log(this.editForm);
      let res = await Edit_rights(this.editForm);
      this.getRolesList();
      this.dialogEditVisible = false;
      console.log(res);
    },
  },
  mounted() {
    this.getRolesList();
  },
};
</script>
<style lang="scss" scoped>
.el-button {
  margin: 10px 0;
}
.box {
  width: 100%;
  display: flex;
  border: 1px solid black;
}
.box_left {
  width: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-right: 1px solid black;
}
.box_right_left {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 200px;
  border-right: 1px solid black;
  border-bottom: 1px solid black;
}
.box_right {
  flex: 1;
  .box_right_item {
    display: flex;
  }
}
.box_right_right {
  flex: 1;
  border-bottom: 1px solid black;
}
.el-tag {
  margin: 15px;
}
.border_bottom_none {
  border-bottom: none;
}
</style>